package rexsee.device;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;

public class RexseeSpecialKey implements JavascriptInterface {

	public static final String INTERFACE_NAME = "SpecialKey";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return new RexseeSpecialKey(childBrowser);
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeSpecialKey(childBrowser);
	}

	private final Browser mBrowser;
	public RexseeSpecialKey(Browser browser) {
		mBrowser = browser;
	}

	public static void disableAll(Browser browser) {
		browser.specialKeyListener.backEnabled = false;
		browser.specialKeyListener.menuEnabled = false;
		browser.specialKeyListener.volumeEnabled = false;
		browser.specialKeyListener.searchEnabled = false;
		browser.specialKeyListener.focusEnabled = false;
	}

	// JavaScript Interface
	public void enableBackKeyListener(boolean enable) {
		mBrowser.specialKeyListener.backEnabled = enable;
	}
	public void setBackKeyListener() {
		setBackKeyListener(null);
		setBackKeyUpListener(null);
	}
	public void setBackKeyListener(final String url) {
		mBrowser.specialKeyListener.back_DownRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}
	public void setBackKeyUpListener(final String url) {
		mBrowser.specialKeyListener.back_UpRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}

	public void enableMenuKeyListener(boolean enable) {
		mBrowser.specialKeyListener.menuEnabled = enable;
	}
	public void setMenuKeyListener() {
		setMenuKeyListener(null);
		setMenuKeyUpListener(null);
	}
	public void setMenuKeyListener(final String url) {
		mBrowser.specialKeyListener.menu_DownRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}
	public void setMenuKeyUpListener(final String url) {
		mBrowser.specialKeyListener.menu_UpRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}

	public void enableSearchKeyListener(boolean enable) {
		mBrowser.specialKeyListener.searchEnabled = enable;
	}
	public void setSearchKeyListener() {
		setSearchKeyListener(null);
		setSearchKeyUpListener(null);
	}
	public void setSearchKeyListener(final String url) {
		mBrowser.specialKeyListener.search_DownRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}
	public void setSearchKeyUpListener(final String url) {
		mBrowser.specialKeyListener.search_UpRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}

	public void enableFocusKeyListener(boolean enable) {
		mBrowser.specialKeyListener.focusEnabled = enable;
	}
	public void setFocusKeyListener() {
		setFocusKeyListener(null);
		setFocusKeyUpListener(null);
	}
	public void setFocusKeyListener(final String url) {
		mBrowser.specialKeyListener.focus_DownRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}
	public void setFocusKeyUpListener(final String url) {
		mBrowser.specialKeyListener.focus_UpRunnable = (url == null || url.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
	}

	public void enableVolumeKeyListener(boolean enable) {
		mBrowser.specialKeyListener.volumeEnabled = enable;
	}
	public void setVolumeKeyListener() {
		setVolumeKeyListener(null, null);
		setVolumeKeyUpListener(null, null);
	}
	public void setVolumeKeyListener(final String volumeUpUrl, final String volumeDownUrl) {
		mBrowser.specialKeyListener.volumeUp_DownRunnable = (volumeUpUrl == null || volumeUpUrl.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(volumeUpUrl);
			}
		};
		mBrowser.specialKeyListener.volumeDown_DownRunnable = (volumeDownUrl == null || volumeDownUrl.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(volumeDownUrl);
			}
		};
	}
	public void setVolumeKeyUpListener(final String volumnUpUrl, final String volumeDownUrl) {
		mBrowser.specialKeyListener.volumeUp_UpRunnable = (volumnUpUrl == null || volumnUpUrl.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(volumnUpUrl);
			}
		};
		mBrowser.specialKeyListener.volumeDown_UpRunnable = (volumeDownUrl == null || volumeDownUrl.equals("")) ? null : new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(volumeDownUrl);
			}
		};
	}

}
